<template>
	<view class="content">
		<view class="main">
			<view class="item" v-for="(item,index) in list">
				<view class="area1">
					<view class="img">
						<image src="/static/my/addr.png" mode="widthFix"></image>
					</view>
					<view class="info">
						<view class="info1">
							<text class="text1">{{item.contacts}}</text>
							<text class="text2">{{item.mobile}}</text>
						</view>
						<view class="info2">
							<text>{{item.addr}} {{item.detail_addr}}</text>
						</view>
					</view>
				</view>
				<view class="cz">
					<view class="left">
						<view class="mr" v-if="item.ismr=='1'" @tap="qxmr(index)">
							<image src="/static/my/gx.png" mode="widthFix"></image>
							<text class="text1">取消默认</text>
						</view>
						<view class="szmr" v-else @tap="setmr(index)">
							<view class="iconfont icon-weixuanzhong icon"></view>
							<text class="text1">设为默认</text>
						</view>
					</view>
					<view class="right">
						<text class="text1" @tap="del(index)">删除</text>
						<text class="text2" @tap="goNext('/pages/my/addr-edit?type=2&addrId='+item.id)">编辑</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn" @tap="goNext('/pages/my/addr-edit?type=1')">
			<text>新增地址</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				pagesize: 10,
				total: 0,
				list: [],
			}
		},
		onLoad() {
			this.getList()
		},
		//触底的事件
		onReachBottom() {
			//判断是否加载完毕
			if(this.page * this.pagesize >= this.total){
				return
			}
			// 让页码值自增
			this.page += 1
			// 重新获取列表数据
			this.getList()
		},
		// 下拉刷新的事件
		onPullDownRefresh() {
			// 1. 重置关键数据
			this.page = 1
			this.total = 0
			this.list = []
					 
			// 2. 重新发起请求
			this.getList()
			//停止刷新
			uni.stopPullDownRefresh()
		},
		methods: {
			//获取列表
			async getList(){
				let data = await this.$request({
						url: '/d/addr/index',
						data: {
							page: this.page,
							pagesize: this.pagesize,
						},
				});
				this.total = data.data.total
				this.list = [...this.list, ...data.data.list]
			},
			//删除
			del(index){
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定删除吗？',
					confirmColor: '#895FFF',
					success: function (res) {
						if (res.confirm) {
							that.del2(index)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			async del2(index){
				let data = await this.$request({
						url: '/d/addr/del',
						method: 'POST',
						data: {
							addrId: this.list[index].id
						},
				});
				//更新本地数据
				this.list.splice(index,1)
				//提示
				uni.showToast({
					icon:"success",
					title:"删除成功"
				})
			},
			//设置默认
			async setmr(index){
				let data = await this.$request({
						url: '/d/addr/setmr',
						data: {
							addrId: this.list[index].id
						},
				});
				//更新本地数据
				this.list.forEach((item,index2)=>{
					if(index2==index){
						item.ismr = '1'
					}else{
						item.ismr = '0'
					}
				})
				//提示
				uni.showToast({
					icon:"success",
					title:"设置成功"
				})
			},
			//取消默认
			async qxmr(index){
				let data = await this.$request({
						url: '/d/addr/qxmr',
						data: {
							addrId: this.list[index].id
						},
				});
				//更新本地数据
				this.list[index].ismr = '0'
				//提示
				uni.showToast({
					icon:"success",
					title:"设置成功"
				})
			},
			//跳转
			goNext(url){
				uni.navigateTo({
					url:url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		.main{
			.item{
				background-color: #fff;
				margin-bottom: 20rpx;
				.area1{
					display: flex;
					padding: 30rpx;
					border-bottom: 1rpx solid #F8F8F8;
					.img{
						width: 60rpx;
						image{
							width: 60rpx;
						}
					}
					.info{
						margin-left: 20rpx;
						.info1{
							line-height: 40rpx;
							.text1{
								font-size: $uni-font-size-lg;
							}
							.text2{
								margin-left: 20rpx;
								color: #999;
							}
						}
						.info2{
							margin-top: 10rpx;
							line-height: 30rpx;
						}
					}
				}
				.cz{
					display: flex;
					padding: 30rpx;
					line-height: 40rpx;
					.left{
						.mr{
							display: flex;
							image{
								margin-top: 2rpx;
								width: 35rpx;
							}
							.text1{
								margin-left: 10rpx;
							}
						}
						.szmr{
							display: flex;
							.icon{
								font-size: 36rpx;
							}
							.text1{
								margin-left: 10rpx;
							}
						}
					}
					.right{
						margin-left: auto;
						color: #ABABAB;
						.text2{
							margin-left: 60rpx;
						}
					}
				}
			}
		}
		.btn{
			position: fixed;
			left: 25rpx;
			bottom: 50rpx;
			width: 700rpx;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 45rpx;
			text-align: center;
			background-color: #895FFF;
			font-size: $uni-font-size-lg;
			letter-spacing: 5rpx;
			color: #fff;
		}
	}
</style>
<style>
	page{
		background-color: #F6F6F6;
	}
</style>
